using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PocketApps
{
    public partial class WebBrowser : Form
    {
        private string url;

        enum browserState
        {
            bLoaded,
            bNavigating,
            bDocumentDone,
        }

        browserState currentState = browserState.bLoaded;
        public string GotoUrl
        {
            get { return url; }
            set { url = value; }
        }

        public WebBrowser()
        {
            InitializeComponent();
        }

        public WebBrowser(string url)
        {
            InitializeComponent();
            GotoUrl = url;
        }

        private void WebBrowser_Load(object sender, EventArgs e)
        {
            miStopBack.Text = "Go";
            //this.webBrowser1.Navigate(new Uri(this.GotoUrl));
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            currentState = browserState.bNavigating;
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {

        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.CanGoBack)
                miStopBack.Text = "Back";
            else
                miStopBack.Text = "Refresh";
            currentState = browserState.bDocumentDone;
        }

        private void miStopBack_Click(object sender, EventArgs e)
        {
            switch (currentState)
            {
                case browserState.bLoaded:
                    this.webBrowser1.Navigate(new Uri(this.GotoUrl));
                    currentState = browserState.bNavigating;
                    break;

                case browserState.bNavigating:
                    this.webBrowser1.Stop();
                    currentState = browserState.bLoaded;

                    if (webBrowser1.CanGoBack)
                        miStopBack.Text = "Back";
                    else
                        miStopBack.Text = "Refresh"; 
                    break;

                case browserState.bDocumentDone:
                    this.miStopBack.Text = "Stop";
                    this.webBrowser1.Navigate(new Uri(this.GotoUrl));
                    currentState = browserState.bNavigating;
                    break;
                default:
                    break;
            }
        }
    }
}